package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.database.model.BookInfo;
import com.shuqi.model.bean.LimitFreeBookTypeInfo;

import com.shuqi.model.parse.HandlerBase;

/**
 * 限时免费解析数据
 * 
 * @author zhuchen
 * 
 */
public class LimitFreeParser extends HandlerBase {

    private List<BookInfo> list; // 书籍信息集合

    private LimitFreeBookTypeInfo bookTypeInfo;
    private BookInfo bookInfo;
    private int inWhich;
    private static final int BOOKNAME = 1;
    private static final int AUTHOR = 2;
    private static final int BOOKTYPE = 3;
    private static final int BOOKDESCRIPTION = 4;

    @Override
    public Object getParsedData() {
        // TODO Auto-generated method stub
        return bookTypeInfo;
    }

    @Override
    public void startDocument() throws SAXException {
        list = new ArrayList<BookInfo>();
        bookTypeInfo = new LimitFreeBookTypeInfo();
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {

        if ("Books".equals(localName)) {
            bookTypeInfo.setColumnId(isNull(attributes, "columnId"));
            bookTypeInfo.setTitle(isNull(attributes, "title"));
            bookTypeInfo.setPageCount(isNull(attributes, "pageCount"));
            bookTypeInfo.setTotalCount(isNull(attributes, "totalCount"));
            bookTypeInfo.setList(list);
        } else if ("Book".equals(localName)) {
            bookInfo = new BookInfo();

            bookInfo.setBookId(isNull(attributes, "bookId"));
            bookInfo.setBookCoverImgUrl(isNull(attributes, "imgUrl"));
            bookInfo.setBookHideState(isNull(attributes, "hide"));
            String coverState = isNull(attributes, "cr_cover_isopen");
            if (coverState != null) {
                try {
                    bookInfo.setCoverHideState(Integer.valueOf(coverState));
                } catch (Exception e) {

                }
            }
            bookInfo.setBookStatus("");// 为添加缓存避免报空指针错误添加
            bookInfo.setDiscount("");// 为添加缓存避免报空指针错误添加

            list.add(bookInfo);
        } else if (localName.equals("BookName")) {
            this.inWhich = BOOKNAME;
        } else if (localName.equals("Author")) {
            this.inWhich = AUTHOR;
        } else if (localName.equals("BookType")) {
            this.inWhich = BOOKTYPE;
        } else if (localName.equals("Description")) {
            this.inWhich = BOOKDESCRIPTION;
        } else {
            this.inWhich = 0;
        }
        super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (value != null && !value.trim().equals("")) {
            switch (this.inWhich) {
                case BOOKNAME:
                    this.bookInfo.setBookName((bookInfo.getBookName() == null ? "" : bookInfo
                            .getBookName()) + value);
                    break;
                case AUTHOR:
                    this.bookInfo.setBookAuthorName((bookInfo.getBookAuthorName() == null ? ""
                            : bookInfo.getBookAuthorName()) + value);
                    break;
                case BOOKTYPE:
                    this.bookInfo.setBookType((bookInfo.getBookType() == null ? "" : bookInfo
                            .getBookType()) + value);
                    break;
                case BOOKDESCRIPTION:
                    this.bookInfo.setBookIntro((bookInfo.getBookIntro() == null ? "" : bookInfo
                            .getBookIntro()) + value);
                    break;
            }
        }
    }

    @Override
    public void endDocument() throws SAXException {

        super.endDocument();
    }
}
